package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import vcp.client.utility.Common;
import vcp.client.utility.UtilityHelper;
import vcp.enums.Screen;
import vcp.enums.SiteEnum;
/**
 * <p> user interface On Site screen class </p>
 * @author VCP team
 *
 */
public class UIOnSiteView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnCheckIn;
	private JButton btnCheckOut;
	private JComboBox <SiteEnum> cbSiteNames;
	/**
	 * constructor
	 */
	public UIOnSiteView ()
	{
		super();
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/

	@Override
	protected void setupLayout ()
	{
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.WEST);

		JPanel panel_11 = new JPanel();
		panel_11.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Sites", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(panel_11);

		this.cbSiteNames = new JComboBox <>();
		panel_11.add(this.cbSiteNames);
		this.cbSiteNames.setModel(new DefaultComboBoxModel <SiteEnum>(SiteEnum.values()));

		JPanel panel_1 = new JPanel();
		this.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new GridLayout(3, 0, 0, 0));

		JPanel panel_5 = new JPanel();
		panel_3.add(panel_5);

		JPanel panel_4 = new JPanel();
		panel_3.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_6 = new JPanel();
		panel_4.add(panel_6);

		this.btnCheckIn = new JButton(Common.BTN_CHECK_IN);
		panel_4.add(this.btnCheckIn);
		this.btnCheckIn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnCheckIn.setName(Common.BTN_CHECK_IN);
		this.btnCheckIn.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		this.btnCheckIn.setFocusPainted(false);

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(3, 0, 0, 0));

		JPanel panel_8 = new JPanel();
		panel_2.add(panel_8);

		JPanel panel_7 = new JPanel();
		panel_2.add(panel_7);
		panel_7.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_9 = new JPanel();
		panel_7.add(panel_9);

		this.btnCheckOut = new JButton(Common.BTN_CHECK_OUT);
		panel_7.add(this.btnCheckOut);
		this.btnCheckOut.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnCheckOut.setName(Common.BTN_CHECK_OUT);
		this.btnCheckOut.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		this.btnCheckOut.setFocusPainted(false);

		JPanel panel_10 = new JPanel();
		panel_10.setPreferredSize(new Dimension(100, 10));
		this.add(panel_10, BorderLayout.EAST);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnCheckIn.addActionListener(this);
		this.btnCheckOut.addActionListener(this);

		this.cbSiteNames.addItemListener(new ItemListener()
		{
			@Override
			public void itemStateChanged ( ItemEvent itemSelected )
			{
				if ( itemSelected.getStateChange() == ItemEvent.SELECTED )
				{
					if ( UIOnSiteView.this.controllerDelegate != null )
					{
						SiteEnum site = (SiteEnum) itemSelected.getItem();
						UIOnSiteView.this.controllerDelegate.onButtonPress(site);
					}
				}
			}
		});
	}

	@Override
	protected void setupButtonsIcons ()
	{
		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CHECK_IN, 48, 48);
		if ( icon != null )
		{
			this.btnCheckIn.setIcon(icon);
			this.btnCheckIn.setIconTextGap(20);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CHECK_OUT, 48, 48);
		if ( icon != null )
		{
			this.btnCheckOut.setIcon(icon);
			this.btnCheckOut.setIconTextGap(20);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton demoViewButton = (JButton) actionEvent.getSource();
		Screen newScreen = null;
		switch ( demoViewButton.getName() )
		{
			case Common.BTN_CHECK_IN:
				newScreen = Screen.CHECK_IN;
				break;
			case Common.BTN_CHECK_OUT:
				newScreen = Screen.CHECK_OUT;
				break;
		}

		if ( newScreen != null )
		{
			this.controllerDelegate.onButtonPress(newScreen);
		}
	}
}
